counting the number of connected components in an undirected graph